package cc.minos.servers.openfire.managers
{
	import cc.minos.servers.openfire.chats.ChatMessage;
	import cc.minos.servers.openfire.chats.SimpleChat;
	import cc.minos.utils.RandomUtil;
	import cc.minos.utils.StringUtil;
	import org.igniterealtime.xiff.core.UnescapedJID;
	import org.igniterealtime.xiff.core.XMPPConnection;
	import org.igniterealtime.xiff.data.im.RosterItemVO;
	import org.igniterealtime.xiff.data.Message;
	import org.igniterealtime.xiff.events.MessageEvent;
	
	/**
	 * ...
	 * @author Minos
	 */
	public class MessageManager extends BasicManager
	{
		static public const TYPE_CODE:String = "typeCode";
		
		public function MessageManager( con:XMPPConnection )
		{
			super( con );
			init();
		}
		
		private function init():void
		{
			con.addEventListener( MessageEvent.MESSAGE , onMessage );
		}
		
		public function rumCommand( message:String ):Boolean
		{
			if ( !message.charAt( 0 ) == '/' )
				return false;
			var segments:Array = message.split( " " );
			switch (( segments.shift() as String ).substring( 1 ) )
			{
				case "chatTo": 
					//trace( "to:" + segments );
					return true;
				case "clearChat":
					break;
			}
			return false;
		}
		
		private function onMessage( e:MessageEvent ):void
		{
			var message:Message = e.data as Message;
			var chat:SimpleChat;
			var body:String;
			try
			{
				/*Debug.trace( "message: " + message.body );
				Debug.trace( "id: " + message.id );
				Debug.trace( "type: " + message.type );
				Debug.trace( "to: " + message.to.toString() );
				Debug.trace( "from: " + message.from.toString() );*/
			}
			catch ( error:Error )
			{
			}
			if ( !message || !message.body ) return;
			switch ( message.type )
			{
				case Message.TYPE_CHAT: 
				case Message.TYPE_GROUPCHAT: 
					
					//trace( message.type + ": " + message.from );
					
					if ( rumCommand( message.body ) )
						return;
					//get group chat by from.jid
					if ( message.from && message.body )
					{
						var vo:RosterItemVO = RosterItemVO.get( message.from.unescaped );
						if ( vo )
						{
							//chat = OpenFireManager.chatManager.getChat( vo.jid );
							chat = OpenFireManager.chatManager.startChat( vo.jid , false );
						}
						else
						{
							chat = OpenFireManager.chatManager.joinGroupChat( message.from.unescaped , false );
						}
						
						if ( !chat ) {
							//start new chat??
							//chat = OpenFireManager.chatManager.startChat( vo.jid , false );
							//return;
						}
						
						//add to message show
						var msg:ChatMessage = ChatMessage.fromMessage( message , chat );
						msg.nick = vo?vo.nickname ? vo.nickname : message.from.resource:message.from.resource;
						chat.ui.addMessage( msg );
						//save message
						OpenFireManager.historyManager.save( msg );
					}
					else
					{
						return;
					}
					break;
				case Message.TYPE_NORMAL: 
					break;
				default: 
			}
		}
		
		public function sendMessage( jid:UnescapedJID , body:String , type:String = null ):void
		{
			//trace( "sendMessage" , jid , body , type );
			var id:String = RandomUtil.randomString( 8 );
			var message:Message = new Message( jid.escaped , id , body , null , type );
			con.send( message );
		}
	
	}

}